//
//  XCRefreshControl.swift
//  小陈微博Swift用纯代码创建
//
//  Created by 小陈 on 16/3/7.
//  Copyright © 2016年 小陈. All rights reserved.
//

import UIKit

class XCRefreshControl: UIRefreshControl {
    // 箭头的偏移值
    private let RefreshControloffest: CGFloat = -60
   
    /// 标示位
    private var isUp = false
    
    override init(){
        super.init()
        
        prepareUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
/// 覆盖父类的方法 监听下拉刷新frame 值的改变
   override var frame: CGRect {
    didSet{
        
                if frame.origin.y > 0{
                    return
                }
        
                if frame.origin.y <= RefreshControloffest && !isUp {
                    print("箭头向上")
                    isUp = true
                    refreshView.rotationTipIcon(isUp)
                    
                }else if frame.origin.y > RefreshControloffest && isUp {
                    print("箭头向下")
                    isUp = false
                    refreshView.rotationTipIcon(isUp)
                }
        /// 判断是否要刷新
        if refreshing{
            refreshView.startRotationAnim()
        }
        
    }
}
    
    func prepareUI(){
         addSubview(refreshView)
        refreshView.snp_makeConstraints { (make) -> Void in
            make.size.equalTo(refreshView.frame.size)
            make.centerX.centerY.equalTo(self)
        }
    }

    
   
    /// 重写父类的 结束刷新的方法
    override func endRefreshing() {
        super.endRefreshing()
        
        refreshView.StopAnim()
    }
    
    
    
   private lazy var refreshView:XCRefreshView = XCRefreshView.refreshView()
}
